package com.xiaoyu.izf.service;

import com.alibaba.fastjson.JSONObject;
import com.xiaoyu.izf.config.SysConfig;
import com.xiaoyu.izf.util.DateTimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class UploadService {

    public JSONObject uploadImg(HttpServletRequest request, MultipartFile file){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 200);
        String imgExt = SysConfig.image;
        try {
            if(file.isEmpty()){
                jsonObject.put("status", 500);
                jsonObject.put("msg", "上传文件不能为空");
                return jsonObject;
            }
            String fileExtName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
            if(StringUtils.isEmpty(fileExtName)){
                jsonObject.put("status", 500);
                jsonObject.put("msg", "上传文件无扩展名");
                return jsonObject;
            }else if(imgExt.indexOf(fileExtName.toLowerCase())<0){
                jsonObject.put("status", 500);
                jsonObject.put("msg", "图片格式不正确，只支持gif,jpg,jpeg,png,bmp格式图片");
                return jsonObject;
            }
            String realPath = request.getSession().getServletContext().getRealPath("");
            String fileName = DateTimeUtil.getDateTime2Str3S4() + "." + fileExtName;
            String imgPath = "upload/img/"+ DateTimeUtil.getDate2StrM2()+"/"+fileName;
            //String imgPath = "upload"+File.separatorChar+"advImg" + File.separatorChar + fileName;
            String imgPathRealPath = realPath + File.separatorChar+ "upload"+File.separatorChar+"img" + File.separatorChar + DateTimeUtil.getDate2StrM2() + File.separatorChar;
            File fi = new File(imgPathRealPath);

            if (!fi.exists()) {
                fi.mkdirs();
            }
            file.transferTo(new File(fi,fileName));
            jsonObject.put("status", 200);
            jsonObject.put("img", imgPath);
            return jsonObject;
        } catch (Exception e) {
            System.out.println("upload error:"+e);
            jsonObject.put("status", 500);
            jsonObject.put("msg", "上传图片错误"+e);
            return jsonObject;
        }
    }
    public String uploadFile(HttpServletRequest request,String file_name){
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            // 获得文件：
            MultipartFile file = multipartRequest.getFile(file_name);
            if(file==null){
                return null;
            }
            String fileExtName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
            if(StringUtils.isEmpty(fileExtName)){
                return "";
            }
            String realPath = request.getSession().getServletContext().getRealPath("");
            String fileName = DateTimeUtil.getDateTime2Str3S4() + "." + fileExtName;
            String imgPath = "upload/img/"+ DateTimeUtil.getDate2StrM2()+"/"+fileName;
            //String imgPath = "upload"+File.separatorChar+"advImg" + File.separatorChar + fileName;
            String imgPathRealPath = realPath + File.separatorChar+ "upload"+File.separatorChar+"img" + File.separatorChar + DateTimeUtil.getDate2StrM2() + File.separatorChar + fileName;
            File fi = new File(imgPathRealPath);

            if (!fi.exists()) {
                fi.mkdirs();
            }
            file.transferTo(fi);

            return imgPath;
        } catch (Exception e) {
            System.out.println("upload error:"+e);
            return "";
        }
    }

    public List<String> uploads(HttpServletRequest request,String paramName){
        List<String> list = new ArrayList<String>();
        try {
            String host = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            // 获得文件：
            List<MultipartFile> files = multipartRequest.getFiles(paramName);
            for(MultipartFile file:files){
                if(!file.isEmpty()){
                    String fileExtName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
                    String realPath = request.getSession().getServletContext().getRealPath("");
                    String uuid = UUID.randomUUID().toString();
                    //String fileName = DateTimeUtil.getDateTime2Str3S4() + "." + fileExtName;
                    String fileName = uuid + "." + fileExtName;
                    String fileSavePath = "upload/img/"+ DateTimeUtil.getDate2StrM2() + "/" + fileName;
                    String fileSavePathRealPath = realPath + File.separatorChar+ "upload"+File.separatorChar+"img"+File.separatorChar+ DateTimeUtil.getDate2StrM2() + File.separatorChar + fileName;
                    File fi = new File(fileSavePathRealPath);
                    if (!fi.exists()) {
                        fi.mkdirs();
                    }
                    file.transferTo(fi);
                    list.add(host+fileSavePath);
                }
            }
        } catch (Exception e) {
            System.out.println("upload error:"+e);
        }
        return list;
    }
}
